package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class OverScrollView extends LinearLayout {

    private Scroller mScroller;

    public OverScrollView(Context context) {
        this(context, null);
    }

    public OverScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OverScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mScroller = new Scroller(context);
    }

    public void smoothScrollToPosition(int finalX, int finalY) {
        int offsetX = finalX - mScroller.getFinalX();
        int offsetY = finalY - mScroller.getFinalY();
        smoothScrollByPosition(offsetX, offsetY);
    }

    private void smoothScrollByPosition(int offsetX, int offsetY) {
        System.out.println("需要的位移："+ offsetY);
        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), offsetX, offsetY);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if(mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
        super.computeScroll();
    }
}
